package net.araim.bbingo.dialogs;

import net.araim.bbingo.R;
import net.araim.bbingo.Shame;
import net.araim.bbingo.db.BingoDB;
import net.araim.bbingo.interfaces.IDialogDismissListener;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ShameDialog extends AlertDialog implements DialogInterface.OnKeyListener {

	public final static int DISMISS_USER = 1;

	private BingoDB db;
	private View v;
	private IDialogDismissListener dialogDismissListener;

	private void FillShames() {
		Shame[] shames = db.getAllshame();
		TableLayout tl = (TableLayout) v.findViewById(R.id.ShameList);
		for (int i = 1; i <= 10; i++) {
			TableRow tr = (TableRow) tl.getChildAt(i);
			if (shames.length >= i) {
				((TextView) tr.findViewById(R.id.RowName)).setText(shames[i - 1].getMeetingName());
				((TextView) tr.findViewById(R.id.RowDate)).setText(shames[i - 1].getMeetingDate());
				((TextView) tr.findViewById(R.id.RowDuration)).setText(String.valueOf(shames[i - 1].getTimeFormated()));
			} else {
				((TextView) tr.findViewById(R.id.RowName)).setText("");
				((TextView) tr.findViewById(R.id.RowDate)).setText("");
				((TextView) tr.findViewById(R.id.RowDuration)).setText("");
			}
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		FillShames();
	}

	private void CreateLayout() {
		final Context ctx = getContext();
		v = LayoutInflater.from(ctx).inflate(R.layout.shamelayout, null);
		this.setView(v);

		// TableLayout tl = (TableLayout) v.findViewById(R.id.ShameList);
		// TableRow tr = (TableRow)
		// LayoutInflater.from(ctx).inflate(R.layout.shamerow, null);
		TableLayout tl = (TableLayout) v.findViewById(R.id.ShameList);

		TableRow tr = (TableRow) LayoutInflater.from(ctx).inflate(R.layout.shamerow, null);
		tr.setBackgroundColor(android.R.color.white);
		((TextView) tr.findViewById(R.id.RowNumber)).setText("#");
		((TextView) tr.findViewById(R.id.RowName)).setText(R.string.RowName);
		((TextView) tr.findViewById(R.id.RowDate)).setText(R.string.RowDate);
		((TextView) tr.findViewById(R.id.RowDuration)).setText(R.string.RowTime);
		tl.addView(tr);
		for (int i = 1; i <= 10; i++) {
			tr = (TableRow) LayoutInflater.from(ctx).inflate(R.layout.shamerow, null);
			if (i % 2 == 0) {
				tr.setBackgroundColor(android.R.color.background_dark);
			} else {
				tr.setBackgroundColor(android.R.color.background_light);
			}
			((TextView) tr.findViewById(R.id.RowNumber)).setText(i + ".");
			tl.addView(tr);
		}

		v.findViewById(R.id.ButtonCloseShame).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dismiss();
				if (ShameDialog.this.dialogDismissListener != null) {
					ShameDialog.this.dialogDismissListener.notifyDismiss(ShameDialog.this, DISMISS_USER);
				}
			}
		});
		v.findViewById(R.id.ButtonResetShame).setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				new AlertDialog.Builder(ctx).setTitle(R.string.ResetConfirmTitle).setMessage(R.string.ResetConfirmMessage)
						.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								db.resetShames();
								FillShames();
							}
						}).setNegativeButton(R.string.No, new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int which) {
								dismiss();
							}
						}).show();
			}
		});
	}

	public ShameDialog(Context context, BingoDB db) {
		super(context);
		this.db = db;
		CreateLayout();
		this.setOnKeyListener(this);
	}

	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			dismiss();
			if (ShameDialog.this.dialogDismissListener != null) {
				ShameDialog.this.dialogDismissListener.notifyDismiss(ShameDialog.this, DISMISS_USER);
			}
			return true;
		}
		return false;
	}

	public void setDialogDismissListener(IDialogDismissListener dialogDismissListener) {
		this.dialogDismissListener = dialogDismissListener;
	}

}
